﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.HitachiServiceRef;
using HitachiLib.Data;
using HitachiLib.Utils;

namespace HitachiApp.Model
{
    public class ProjectListViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<ViewItem> prj_list = new ObservableCollection<ViewItem>();
        private Project parent = null;
        private bool show_all = true;
        private ScheduleTabViewModel parent_model;

        public ProjectListViewModel(ScheduleTabViewModel parent_m)
        {
            parent_model = parent_m;
        }

        public bool ShowAll
        {
            get { return show_all; }
            set { show_all = value; }
        }
        public bool ShowUnScheduledOnly
        {
            get { return !ShowAll; }
            set { ShowAll = !value; }
        }
        public ObservableCollection<ViewItem> ProjectList
        {
            get { return prj_list; }
        }
        public int Count
        {
            get { return prj_list.Count; }
        }
        public Project Parent
        {
            get { return parent; }
            set
            {
                if (value is Project)
                {
                    prj_list.Clear();
                    prj_list = new ObservableCollection<ViewItem>();
                    foreach (Project p in value.Children.Values)
                    {
                        if (show_all)
                        {
                            ViewItem vi = new ViewItem(p, true);
                            prj_list.Add(vi);
                        }
                        else
                        {
                            if (!p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
                            {
                                ViewItem vi = new ViewItem(p, true);
                                prj_list.Add(vi);
                            }
                        }
                    }
                    RaisePropertyChanged("ProjectList");
                }
            }
        }

        public bool ShouldContains(Project p)
        {
            if (p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
                return false;
            else
                return true;
        }

        public void ApplyToProject(ViewItem vi)
        {
            Project p = vi.Project;
            if (ShouldContains(p))
                return;
            if (p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
            {
                Schedule s = p.PlanSchedule[Schedule.HalfMonth];
                HitachiServiceClient client = WCFClient.GetClient();
                client.FreezeScheduleByProjectIdAsync(p.Id, Schedule.HalfMonth);
                client.FreezeScheduleByProjectIdAsync(p.Id, Schedule.WholeYear);
                p.PlanSchedule.Clear();
            }
            parent_model.Cleanup();
        }

        public void RefreshData()
        {
            RaisePropertyChanged("ProjectList");
        }
        public void RefreshCount()
        {
            parent_model.RefreshCount();
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
